package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.tencentUtils.TencentCloudImUtil;
import com.ruoyi.system.domain.AppGroupUser;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppGroupUserService;
import com.ruoyi.system.service.IAppUserService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppGroupInfoMapper;
import com.ruoyi.system.domain.AppGroupInfo;
import com.ruoyi.system.service.IAppGroupInfoService;

import javax.annotation.Resource;

/**
 * 群组信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-27
 */
@Service
public class AppGroupInfoServiceImpl extends ServiceImpl<AppGroupInfoMapper, AppGroupInfo> implements IAppGroupInfoService {
    @Resource
    private AppGroupInfoMapper appGroupInfoMapper;

    @Autowired
    private IAppGroupUserService iAppGroupUserService;
    @Autowired
    private IAppUserService userService;

    /**
     * 查询群组信息
     *
     * @param id 群组信息主键
     * @return 群组信息
     */
    @Override
    public AppGroupInfo selectAppGroupInfoById(Long id) {
        return appGroupInfoMapper.selectAppGroupInfoById(id);
    }

    /**
     * 查询群组信息列表
     *
     * @param appGroupInfo 群组信息
     * @return 群组信息
     */
    @Override
    public List<AppGroupInfo> selectAppGroupInfoList(AppGroupInfo appGroupInfo) {
        return appGroupInfoMapper.selectAppGroupInfoList(appGroupInfo);
    }

    /**
     * 新增群组信息
     *
     * @param appGroupInfo 群组信息
     * @return 结果
     */
    @Override
    public int insertAppGroupInfo(AppGroupInfo appGroupInfo) {
        appGroupInfo.setCreateTime(DateUtils.getNowDate());
        return appGroupInfoMapper.insertAppGroupInfo(appGroupInfo);
    }

    /**
     * 修改群组信息
     *
     * @param appGroupInfo 群组信息
     * @return 结果
     */
    @Override
    public int updateAppGroupInfo(AppGroupInfo appGroupInfo) {
        return appGroupInfoMapper.updateAppGroupInfo(appGroupInfo);
    }

    /**
     * 批量删除群组信息
     *
     * @param ids 需要删除的群组信息主键
     * @return 结果
     */
    @Override
    public int deleteAppGroupInfoByIds(Long[] ids) {
        return appGroupInfoMapper.deleteAppGroupInfoByIds(ids);
    }

    /**
     * 删除群组信息信息
     *
     * @param id 群组信息主键
     * @return 结果
     */
    @Override
    public int deleteAppGroupInfoById(Long id) {
        return appGroupInfoMapper.deleteAppGroupInfoById(id);
    }

    @Override
    public AjaxResult createGroup(AppGroupInfo groupInfoEntity, String members) {
        groupInfoEntity.setMemberLimit(2000);
        this.insertAppGroupInfo(groupInfoEntity);

        List<AppGroupUser> groupUserList = Lists.newArrayList();
        if (StringUtils.isNotBlank(members)) {
            JSONArray memberArr = JSONArray.parseArray(members);
            AppGroupUser member;
            for (int i = 0; i < memberArr.size(); i++) {
                member = new AppGroupUser();
                member.setCreateTime(groupInfoEntity.getCreateTime());
                member.setGroupId(groupInfoEntity.getGroupId());
                QueryWrapper<AppUser> ew = new QueryWrapper<>();
                ew.eq("account", memberArr.getString(i));
                AppUser appUser = userService.getOne(ew);
                if (appUser != null) {
                    member.setUserId(appUser.getUserId());
                    member.setMobile(appUser.getMobile());
                    iAppGroupUserService.insertAppGroupUser(member);
                }

            }
        }
        return AjaxResult.ok(0, "", null);
    }

    @Override
    public AjaxResult removeGroup(AppGroupInfo groupEntity) {
        QueryWrapper<AppGroupUser> groupUserWrapper = new QueryWrapper<>();
        groupUserWrapper.eq("group_id", groupEntity.getGroupId());
        iAppGroupUserService.remove(groupUserWrapper);

        QueryWrapper<AppGroupInfo> groupWrapper = new QueryWrapper<>();
        groupWrapper.eq("group_id", groupEntity.getGroupId());
        this.remove(groupWrapper);
        return AjaxResult.ok(0, "", null);
    }

    @Override
    public AjaxResult addGroupMember(String groupId, String members) {
        JSONArray memberArr = JSONArray.parseArray(members);
        long count;
        AppGroupUser groupUser;
        List<AppGroupUser> groupUserList = Lists.newArrayList();
        for (int i = 0; i < memberArr.size(); i++) {
            QueryWrapper<AppGroupUser> groupUserWrapper = new QueryWrapper<>();
            groupUserWrapper.eq("group_id", groupId);
            groupUserWrapper.eq("user_id", memberArr.getLong(i));

            count = iAppGroupUserService.count(groupUserWrapper);
            if (count == 0L) {
                groupUser = new AppGroupUser();
                groupUser.setCreateTime(new Date());
                groupUser.setGroupId(groupId);
                groupUser.setUserId(memberArr.getLong(i));
                groupUser.setMobile(userService.getById(memberArr.getLong(i)).getMobile());
                groupUserList.add(groupUser);
            }
        }
        if (groupUserList.size() > 0) {
            iAppGroupUserService.saveBatch(groupUserList);
        }
        return AjaxResult.ok(0, "", null);
    }

    @Override
    public AjaxResult useActiveCode(String groupId, String code) {
        return null;
    }

    @Autowired
    private TencentCloudImUtil tencentCloudImUtil;

    @Override
    public int destroyGroup(Long[] ids) {
        QueryWrapper<AppGroupInfo> groupUserWrapper = new QueryWrapper<>();
        groupUserWrapper.in("id", ids);

        List<AppGroupInfo> list = appGroupInfoMapper.selectList(groupUserWrapper);
        for (AppGroupInfo info : list) {
            // 解散IM群
            tencentCloudImUtil.destroyGroup(info.getGroupId());
        }
        return appGroupInfoMapper.deleteAppGroupInfoByIds(ids);
    }

    @Override
    public int clearGroupAttr(Long[] ids) {
        QueryWrapper<AppGroupInfo> groupUserWrapper = new QueryWrapper<>();
        groupUserWrapper.in("id", ids);

        List<AppGroupInfo> list = appGroupInfoMapper.selectList(groupUserWrapper);
        for (AppGroupInfo info : list) {
            // 解散IM群
            tencentCloudImUtil.clearGroupAttr(info.getGroupId());
        }
        return 0;
    }

    @Override
    public void destroyGroupId(String beGroupId) {
        tencentCloudImUtil.destroyGroup(beGroupId);
        QueryWrapper<AppGroupInfo> ew = new QueryWrapper<>();
        ew.eq("group_id", beGroupId);
        appGroupInfoMapper.delete(ew);
    }

    @Override
    public int selectInfoNum() {
        return appGroupInfoMapper.selectInfoNum();
    }
}
